<?php

class Db
{

    static $instance;

    static function Connect($host, $login, $password, $database) {
        $connectString = sprintf('mysql:host=%s;dbname=%s', $host, $database);
        self::$instance = new PDO($connectString, $login, $password);
        self::getInstance()->exec('SET CHARACTER SET utf8');
    }
    
    static function getInstance() {
        return self::$instance;
    }
    
    static function execute($prepQuery, $valuesArray = null) {
        $query = self::getInstance()->prepare($prepQuery);
        $result = $query->execute($valuesArray);
        return $result;
    }


    static function fetchColumn($prepQuery, $valuesArray = null) {
        $query = self::getInstance()->prepare($prepQuery);
        $query->execute($valuesArray);
        return $query->fetchColumn();
    }

    static function fetch($prepQuery, $valuesArray = null) {
        $query = self::getInstance()->prepare($prepQuery);
        $query->execute($valuesArray);
        return $query->fetch();
    }


    static function fetchAll($prepQuery, $valuesArray = null) {
        $query = self::getInstance()->prepare($prepQuery);
        $query->execute($valuesArray);
        return $query->fetchAll();
    }
    
    static function lastId() {
        return self::getInstance()->lastInsertId();
    }

}